var DoubanApiInfo = {
	api_key: "0ef4acac01a52b9b2cde060404ef82d1",
	api_key_secret: "1d8580bc9b56fdb3",
	request_token: "",
	request_token_secret: "",
	access_token: "",
	access_token_secret: "",
	user_id: "",
	
	signature_method: "PLAINTEXT",
	
	request_token_uri: "http://www.douban.com/service/auth/request_token",
	access_token_uri: "http://www.douban.com/service/auth/access_token",
	authorization_uri: "http://www.douban.com/service/auth/authorize?oauth_token="
}

var DoubanApi = {

	getMiniblog: function(start, callback, fail_callback) {
		var message = {
			method: "GET",
			action: "http://api.douban.com/people/%40me" + 
				"/miniblog/contacts?start-index=" + start + "&max-results=50",
			parameters: {
				oauth_consumer_key: DoubanApiInfo.api_key,
				oauth_token: DoubanApiInfo.access_token,
				oauth_signature_method: DoubanApiInfo.signature_method,
				oauth_signature: "",
				oauth_timestamp: "",
				oauth_nonce: ""
			}
		};
		
		OAuth.setTimestampAndNonce(message);
		OAuth.SignatureMethod.sign(message, {
			consumerSecret: DoubanApiInfo.api_key_secret,
			tokenSecret: DoubanApiInfo.access_token_secret
		});
		
		var oauth_header = "OAuth realm=\"\", oauth_consumer_key=";
        oauth_header += message.parameters.oauth_consumer_key + ', oauth_nonce=';
        oauth_header += message.parameters.oauth_nonce + ', oauth_timestamp=';
        oauth_header += message.parameters.oauth_timestamp + ', oauth_signature_method=PLAINTEXT, oauth_signature=';
        oauth_header += message.parameters.oauth_signature + ', oauth_token=';
        oauth_header += message.parameters.oauth_token;
        
        Application.console.log("doubanbuddy: crawling " + message.action);
        jQuery.ajax({
        	url: message.action,
        	type: message.method,
        	dataType: "xml",
        	beforeSend: function(request) {
				request.setRequestHeader("Authorization", oauth_header);
			},
			success: function(data) {
//				Application.console.log(data);
				if (typeof callback == 'function' ) callback(data);
			}, 
			error: function(data) {
				if (typeof fail_callback == 'function' )  fail_callback(data);
			}
        });
	},
	
	getSubjectInfo: function(link, kind, id, callback) {
		var kindMap = {
			"book" : "book/subject/",
			"movie" : "movie/subject/",
			"music" : "music/subject/",
			"friend" : "people/",
			"contact" : "people/",
			"event" : "event/",
			"group" : "group/",
			"note" : "note/",
			"review" : "review/"
		};
		
		if (kindMap[kind] == null)
			return;
			
		var apiAction = "http://api.douban.com/" + kindMap[kind] + id + "?apikey=" + DoubanApiInfo.api_key;
		jQuery.ajax({
			url: apiAction,
			type: "GET",
			dataType: "xml",
			success: function(data) {
				if (typeof callback == 'function' ) callback(data, link, kind, id);
			}
		});
	},
	
	getAccessToken: function(callback, failcallback) {
		var message = {
			method: "GET",
			action: DoubanApiInfo.access_token_uri,
			parameters: {
				oauth_consumer_key: DoubanApiInfo.api_key,
				oauth_token: DoubanApiInfo.request_token,
				oauth_signature_method: DoubanApiInfo.signature_method,
				oauth_signature: "",
				oauth_timestamp: "",
				oauth_nonce: ""
			}
		};		
		
		OAuth.setTimestampAndNonce(message);
		OAuth.SignatureMethod.sign(message, {
			consumerSecret: DoubanApiInfo.api_key_secret,
			tokenSecret: DoubanApiInfo.request_token_secret
		});
		
		Application.console.log(message.action + "?" + this.concatParameters(message.parameters));
		jQuery.ajax({
			url: message.action + "?" + this.concatParameters(message.parameters),
			type: message.method,
			dataType: "text",
			success: function(data) {
				Application.console.log("doubanbuddy: " + data);				
				var responseObj = OAuth.getParameterMap(OAuth.decodeForm(data));
				DoubanApiInfo.access_token = responseObj.oauth_token;
				DoubanApiInfo.access_token_secret = responseObj.oauth_token_secret;
				DoubanApiInfo.user_id = responseObj.douban_user_id;
				Application.console.log("doubanbuddy: " + DoubanApiInfo.access_token);
				Application.console.log("doubanbuddy: " + DoubanApiInfo.access_token_secret);
				Application.console.log("doubanbuddy: " + DoubanApiInfo.user_id);
				if (typeof callback == 'function' ) callback();
			},
			error: function(request, status) {
				if (typeof failcallback == 'function') failcallback(request.status);
			}
		});		
	},
	
	getRequestToken: function(callback, failcallback) {
		var message = {
			method: "GET",
			action: DoubanApiInfo.request_token_uri,
			parameters: {
				oauth_consumer_key: DoubanApiInfo.api_key,
				oauth_signature_method: DoubanApiInfo.signature_method,
				oauth_signature: DoubanApiInfo.api_key_secret,
				oauth_timestamp: "",
				oauth_nonce: ""
			}
		};
		
		OAuth.setTimestampAndNonce(message);
		
		Application.console.log(message.action + "?" + this.concatParameters(message.parameters));
		jQuery.ajax({
			url: message.action + "?" + this.concatParameters(message.parameters),
			type: message.method,
			dataType: "text",
			success: function(data) {
				Application.console.log("doubanbuddy: " + data);				
				var responseObj = OAuth.getParameterMap(OAuth.decodeForm(data));
				DoubanApiInfo.request_token = responseObj.oauth_token;
				DoubanApiInfo.request_token_secret = responseObj.oauth_token_secret;
				Application.console.log("doubanbuddy: " + DoubanApiInfo.request_token);
				Application.console.log("doubanbuddy: " + DoubanApiInfo.request_token_secret);
				if (typeof callback == 'function' ) callback();
			},
			error: function(request, status) {
				if (typeof failcallback == 'function') failcallback(request.status);
			}
		});		
	},
	
	concatParameters: function(parameters) {
		var param = "";
		for (var item in parameters) {
			if (param != "")
				param += "&";
			param += item + "=" + escape(parameters[item]);
		}
		return param;
	}
}